﻿using MesBSW.DAL.CustomDB;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesBSW.Tools.DataGirdViewExt
{
    public class DataGridViewUIBreadcrumbColumn : DataGridViewColumn
    {
        //private BreadcrumbModel breadcrumbModel = new BreadcrumbModel();

        //public BreadcrumbModel BreadcrumbModel
        //{
        //    get
        //    {
        //        return breadcrumbModel;
        //    }
        //    set
        //    {
        //        breadcrumbModel = value;
        //    }
        //}

        /// <summary>
        /// 构造方法
        /// </summary>
        public DataGridViewUIBreadcrumbColumn()
        {
            this.CellTemplate = new DataGridViewUIBreadcrumbCell();
        }
    }


    public class DataGridViewUIBreadcrumbCell : DataGridViewCell
    {
        //private BreadcrumbModel breadcrumbModel = new BreadcrumbModel();

        //public BreadcrumbModel BreadcrumbModel
        //{
        //    get
        //    {
        //        return breadcrumbModel;
        //    }
        //    set
        //    {
        //        breadcrumbModel = value;
        //    }
        //}

        public DataGridViewUIBreadcrumbCell()
        {
            ValueType = typeof(BreadcrumbModel);
        }


        public override Type ValueType
        {
            get
            {
                return base.ValueType;
            }
            set
            {
                if (value != null && value != typeof(BreadcrumbModel))
                {
                    throw new ArgumentException("ValueType must be BreadcrumbModel");
                }

                base.ValueType = value;
            }
        }


        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,int rowIndex, 
                                        DataGridViewElementStates elementState, object value, object formattedValue,string errorText, 
                                        DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,DataGridViewPaintParts paintParts)
        {
            UIBreadcrumb tmpPro = new UIBreadcrumb();

            if (value != null)
            {
                BreadcrumbModel breadcrumbModel = value as BreadcrumbModel;
                EntityTools.CopyEntity<BreadcrumbModel, UIBreadcrumb>(breadcrumbModel, ref tmpPro);
                if (breadcrumbModel.ItemString != null && breadcrumbModel.ItemString.Count > 0)
                {
                    tmpPro.Items.AddRange(breadcrumbModel.ItemString.ToArray());
                }
                tmpPro.ItemIndex = breadcrumbModel.ItemIndex;

                tmpPro.Width = tmpPro.Items.Count * tmpPro.ItemWidth;
                tmpPro.Location = new Point(5, 5);
                tmpPro.BackColor = cellStyle.BackColor;
                tmpPro.Size = new Size(cellBounds.Width - 1, cellBounds.Height - 1);
                tmpPro.ForeColor = cellStyle.ForeColor;
            }

            var bmp = new Bitmap(cellBounds.Width, cellBounds.Height);
            var g = Graphics.FromImage(bmp);
            if (value != null)
            {
                tmpPro.DrawToBitmap(bmp, tmpPro.ClientRectangle);
            }
            
            graphics.DrawImage(bmp, cellBounds.Location);

            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue,
                            errorText, cellStyle, advancedBorderStyle, paintParts);
        }
    }
}
